package com.manong.web.controller;

import com.github.pagehelper.PageInfo;
import com.manong.data.ResultData;
import com.manong.domain.Department;
import com.manong.query.DepartmentQueryObject;
import com.manong.query.QueryObject;
import com.manong.service.IDepartmentService;
import com.manong.util.JsonResult;
import com.manong.util.RequiredPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/department")
@CrossOrigin(allowCredentials = "true")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    /**
     * 分页查询接口
     * @param departmentQueryObject
     *
     * @return
     */
    @GetMapping("/list")
    @RequiredPermission(name = "部门列表", expression = "department:list")
    public JsonResult list(DepartmentQueryObject departmentQueryObject) {
        PageInfo pageInfo = departmentService.query(departmentQueryObject);
        // 先解决JsonResult->Data属性解决
        ResultData data = new ResultData(departmentQueryObject.getPageNum(), departmentQueryObject.getPageSize(),pageInfo.getList(),Integer.parseInt(pageInfo.getTotal() + ""));
        return JsonResult.success(data);
    }

    /**
     * 删除部门接口
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    @RequiredPermission(name = "部门删除", expression = "department:delete")
    public JsonResult delete(@PathVariable Long id) {
        departmentService.delete(id);
        return JsonResult.success();
    }

    /**
     * 部门新增或者编辑接口
     * @param department
     * @return
     */
    @PostMapping("/saveOrUpdate")
    @RequiredPermission(name = "部门新增或者编辑", expression = "department:saveOrUpdate")
    public JsonResult saveOrUpdate(@RequestBody Department department) {
        departmentService.saveOrUpdate(department);
        return JsonResult.success();
    }

    /**
     * 查询单个部门
     * @param id
     * @return
     */
    @GetMapping("/getDepartment/{id}")
    @RequiredPermission(name = "查询单个部门", expression = "department:getDepartment")
    public JsonResult getDepartment(@PathVariable Long id) {
        Department department = departmentService.get(id);
        // JsonResult data属性 department
        return JsonResult.success(department);
    }


    /**
     * 查询全部部门
     * @return
     */
    @GetMapping("/getDepartmentList")
    @RequiredPermission(name = "查询全部部门", expression = "department:getDepartmentList")
    public JsonResult getDepartmentList() {
        List<Department> all = departmentService.getAll();
        return JsonResult.success(all);
    }
}
